//
//  SXVideoDetailViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXVideoDetailViewController.h"
#import "SXVideoDetailInfoView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UIImageView+WebCache.h"
#import "UIView+UIView.h"
#import "SXCollectionManager.h"

@interface SXVideoDetailViewController ()

@property (nonatomic, strong) UIImageView *detailImageView;
@property (nonatomic, strong) SXVideoDetailInfoView *infoView; // 下面的详情
@property (nonatomic, strong) MPMoviePlayerViewController *moviePlayerController; // 视频播放控制器

@end

@implementation SXVideoDetailViewController

- (UIImageView *)detailImageView
{
    if (!_detailImageView) {
        _detailImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenWidth * 777 / 1242)];
        [_detailImageView sd_setImageWithURL:[NSURL URLWithString:_detailModel.coverForDetail]];
        _detailImageView.userInteractionEnabled = YES;
        
        UIButton *playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        playButton.frame = CGRectMake(_detailImageView.width / 2 - 30, _detailImageView.height / 2 - 30, 60, 60);
        playButton.backgroundColor = [UIColor clearColor];
        [playButton setBackgroundImage:[UIImage imageNamed:@"SX-bofang"] forState:(UIControlStateNormal)];
        [playButton addTarget:self action:@selector(play) forControlEvents:(UIControlEventTouchUpInside)];
        [_detailImageView addSubview:playButton];
    }
    return _detailImageView;
}

- (SXVideoDetailInfoView *)infoView
{
    if (!_infoView) {
        _infoView = [[SXVideoDetailInfoView alloc] initWithFrame:CGRectMake(0, self.detailImageView.height + 64, kScreenWidth, kScreenHeight - self.detailImageView.height - 64)];
        _infoView.configureModel = _detailModel;
        
        _infoView.collectionButtn.selected = [SXCollectionManager checkCollectionWithTableName:@"video" urlID:_detailModel.title];
        
        [_infoView.collectionButtn addTarget:self action:@selector(collectionVideo:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _infoView;
}

// 收藏
- (void)collectionVideo:(UIButton *)button
{
    button.selected = !button.selected;
    NSString *str = @"收藏成功";
    if (!button.selected) {
        str = @"取消收藏成功";
    }
    NSData *dataName = [self returnDataWithDict:_detailModel.dataDict];
    [SXCollectionManager collectionDataWithTableName:@"video" dataName:dataName urlID:_detailModel.title isCollection:button.selected finish:^(BOOL isSuccessed) {
        if (isSuccessed) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [alertView show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alertView dismissWithClickedButtonIndex:0 animated:NO];
            });
        }
    }];
}

- (NSData *)returnDataWithDict:(NSDictionary *)dict
{
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:dict forKey:_detailModel.title];
    [archiver finishEncoding];
    return data;
}

- (MPMoviePlayerViewController *)moviePlayerController
{
    if (!_moviePlayerController) {
        _moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:_detailModel.playUrl]];
        _moviePlayerController.view.frame = CGRectMake(0, 64, kScreenWidth, kScreenWidth * 777 / 1242);
        _moviePlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        _moviePlayerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
        _moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
        [_moviePlayerController.moviePlayer setFullscreen:NO animated:YES];
    }
    return _moviePlayerController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.navigationItem.title = _detailModel.title;
    
    [self.view addSubview:self.detailImageView];
    [self.view addSubview:self.infoView];

}

// 播放
- (void)play
{
    [self.view addSubview:self.moviePlayerController.moviePlayer.view];
    [self.moviePlayerController.moviePlayer play];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
